package coolq.yaozhenyong.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.forte.qqrobot.sender.MsgSender;

import coolq.yaozhenyong.custom.AjaxResult;
import coolq.yaozhenyong.custom.TimingOnes;
import coolq.yaozhenyong.thread.QqRobot;


@Controller
public class HomeController {
	
	@RequestMapping(value ="/open")
	@ResponseBody
	private Object open(HttpServletResponse response) {
		response.addHeader( "Access-Control-Allow-Origin", "*" );
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String startDate = df.format(new Date());
			/*** 
			 * 给线程指定超时时间，超时则停止线程
			 * 搬运地址 https://www.cnblogs.com/janetshen/p/7976801.html
			 */
			QqRobot task1 = new QqRobot("one", 4);
			Thread t1 = new Thread(task1);
			t1.start();
			try { 
				t1.join(10000); // 在主线程中等待t1执行5秒
			}catch (InterruptedException e) {
				e.printStackTrace();
				return true;
			}
			String endDate = df.format(new Date());
			long starttime = df.parse(startDate).getTime();
			long endtime = df.parse(endDate).getTime();
			int minutes = (int) ((endtime-starttime)/(1000));
			if(minutes<10) {
				return true;
			}else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}
	/****
	 * 关闭
	 * @param response
	 * @return
	 */
	@RequestMapping(value ="/close")
	@ResponseBody
	private Object close(HttpServletResponse response) {
		response.addHeader( "Access-Control-Allow-Origin", "*" );
		System.exit(0);
		Runtime rt = Runtime.getRuntime();
        try {
            rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");//关闭正在运行的cmd窗口
        } catch (Exception e) {
            e.printStackTrace();
        }
		return true;
	}
	@RequestMapping(value="/tosave")
	@ResponseBody
	private Object toxiaoxi(HttpServletResponse response) {
		response.addHeader( "Access-Control-Allow-Origin", "*" );
		System.out.println(" 已运行tosave方法，将会获取机器人加入的QQ群 ");
		TimingOnes.setUpdategrade(2);
		return true;
	}
}
